package com.surveypark.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.surveypark.dao.BaseDao;
import com.surveypark.domain.Question;
import com.surveypark.domain.statistics.OptionStatisticsModel;
import com.surveypark.domain.statistics.QuestionStatisticsModel;
import com.surveypark.service.StatisticsService;

@Service("statisticsService")
public class StatisticsServiceImpl implements StatisticsService {

	@Resource(name = "questionDao")
	private BaseDao<Question> questionDao;

	@Resource(name = "answerDao")
	private BaseDao<Question> answerDao;

	@Override
	public QuestionStatisticsModel statistics(Integer id) {

		QuestionStatisticsModel qsm = new QuestionStatisticsModel();
		Question q = questionDao.getEntity(id);
		qsm.setQuestion(q);
		String hql = "select count(*) from Answer a where a.questionId = ?";
		int qcount = ((Long) answerDao.uniqueResult(hql, id)).intValue();
		qsm.setCount(qcount);

		String oHql = "select count(*) from Answer a where a.questionId = ? and concat(',',a.answerIds,',') like ?";
		int ocount = 0;

		int qt = q.getQuestionType();
		switch (qt) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			String[] optArr = q.getOptionsArr();
			OptionStatisticsModel osm = null;
			for (int i = 0; i < optArr.length; i++) {
				osm = new OptionStatisticsModel();
				osm.setOptionLable(optArr[i]);
				osm.setOptionIndex(i);
				ocount = ((Long) answerDao.uniqueResult(oHql, id, "%," + i
						+ ",%")).intValue();
				osm.setCount(ocount);
				qsm.getOsms().add(osm);
			}
			if (q.isOther()) {
				osm = new OptionStatisticsModel();
				osm.setOptionLable("其他");
				ocount = ((Long) answerDao.uniqueResult(oHql, id, "%other%"))
						.intValue();
				osm.setCount(ocount);
				qsm.getOsms().add(osm);
			}
			break;
		case 6:
		case 7:
		case 8:
			String[] rows = q.getMatrixRowTitlesArr();
			String[] cols = q.getMatrixColTitlesArr();
			String[] opts = q.getMatrixSelectOptionsArr();
			for (int i = 0; i < rows.length; i++) {
				for (int j = 0; j < cols.length; j++) {
					if (qt != 8) {
						osm = new OptionStatisticsModel();
						osm.setMatrixRowLabel(rows[i]);
						osm.setMatrixRowIndex(i);
						osm.setMatrixColLabel(cols[j]);
						osm.setMatrixColIndex(j);
						ocount = ((Long)answerDao.uniqueResult(oHql, id, "%," + i + "_" + j
								+ ",%")).intValue();
						osm.setCount(ocount);
						qsm.getOsms().add(osm);
					} else {
						for(int k = 0; k < opts.length; k++){
							osm = new OptionStatisticsModel();
							osm.setMatrixRowLabel(rows[i]);
							osm.setMatrixRowIndex(i);
							osm.setMatrixColLabel(cols[j]);
							osm.setMatrixColIndex(j);
							osm.setMatrixSelectLabel(opts[k]);
							osm.setMatrixSelectIndex(k);
							String arg = "%," + i + "_" + j + "_" + k + ",%";
									
							ocount = ((Long)answerDao.uniqueResult(oHql, id, arg)).intValue();
							osm.setCount(ocount);
							qsm.getOsms().add(osm);
						}
					}
				}
			}
			break;
		}
		return qsm;
	}
}
